package com.ford.trade.controller;

import com.ford.trade.entity.AoKeGame;
import com.ford.trade.processor.AoKeOddsProcessor;
import com.ford.trade.service.AoKeGameService;
import com.ford.trade.service.OddsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.scheduler.BloomFilterDuplicateRemover;
import us.codecraft.webmagic.scheduler.QueueScheduler;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author :  zhangfutao
 * @version :  V-1.0
 * @description :
 * @date :   2022/5/10 14:15
 */
@Slf4j
@Controller
@Component("trade2")
public class Trade2Controller {


    public static final String url0 = "?page=0&trnum=90&companytype=ExceptExchangeMakerID&type=1";
    public static final String url7 = "";
    public static final String url1 = "?page=0&trnum=30&companytype=ExchangeBooks&type=1";
    public static final String url2 = "?page=0&trnum=8&companytype=AuthoriteBooks&type=1";
    public static final String url3 = "?page=0&trnum=28&companytype=BigBooks&type=1";
    public static final String url4 = "?page=0&trnum=5&companytype=SmallBooks&type=1";
    public static final String url5 = "?page=0&trnum=3&companytype=LeagueBooks&type=1";
    public static final String url6 = "?page=0&trnum=5&companytype=MyOddsBooks2&type=1";


    @Resource
    private AoKeGameService aoKeGameService;
    @Resource
    private OddsService oddsService;

    @RequestMapping("/page/getAokeGames")
    public String getBiSai(@RequestBody @RequestParam(value = "date") String date,
                           ModelMap modelMap) {
        List<AoKeGame> aoKeGameList = aoKeGameService.findAllByDateAfter(date);
        modelMap.addAttribute("list",aoKeGameList);
        return "aokeList";
    }

    @RequestMapping(value = "/page/getOdds")
    @ResponseBody
    public String getOddsPage(@RequestBody
            @RequestParam(value = "indexId") String indexId,
            @RequestParam(value = "url") String url) {
        AoKeGame aoKeGame = aoKeGameService.findByIndexId(Integer.valueOf(indexId));
        if (null == aoKeGame) {
            return "index";
        }
        List<String> urls = new ArrayList<>();
        if ("null".equals(url)){
            urls.add("https://www.okooo.com/soccer/match/" + indexId + "/odds/ajax/");
        }else {
            urls.add("https://www.okooo.com/soccer/match/" + indexId + "/odds/ajax/" + url);
        }
        AoKeOddsProcessor aoKeOddsProcessor = new AoKeOddsProcessor();
        aoKeOddsProcessor.setOddsService(oddsService);

        Request request = new Request();
        Map<String, Object> params = new HashMap<>();
        params.put("page",0);
        params.put("trnum",60);
        params.put("companytype","AuthoriteBooks");
        params.put("type",1);
        request.setExtras(params);
        request.setUrl(urls.get(0));
        Spider.create(aoKeOddsProcessor)
                .addRequest(request)
                .setScheduler(new QueueScheduler().setDuplicateRemover(new BloomFilterDuplicateRemover(100)))
                .thread(1)
                .run();
        return "redirect:/index.html";
    }


}
